package project.web.job;

import java.util.Objects;

import online.model.check.InputCheck;
import online.model.session.SessionUser;
import online.struts.action.BaseAction;
import online.struts.action.UniForm;
import project.check.attribute.LongNumeralCheck;
import project.check.existence.MustCheck;
import project.web.InstanceFactory;
import project.web.generic.SearchAction;
import batch.controller.JobRequestor;
import batch.status.Job;
import batch.status.JobStatus;
import common.db.JdbcSource;
import common.db.jdbc.Jdbc;
import core.config.Factory;

/**
 * バッチ状況照会画面
 *
 * @author Tadashi Nakayama
 * @version 1.0.0
 */
public final class StatusAction extends BaseAction {
	/** JobSeq */
	private static final String JOB_SEQ = "JobSeq";

	/**
	 * 表示処理
	 *
	 * @param model 汎用モデル
	 * @return 処理結果
	 */
	public String get(final UniForm model) {
		return new SearchAction().get(model);
	}

	/**
	 * バッチ中止処理
	 *
	 * @param model 汎用モデル
	 * @return 処理結果
	 */
	public String cancel(final UniForm model) {

		InputCheck ic = InstanceFactory.create(InputCheck.class, model);
		ic.add(JOB_SEQ, new MustCheck());
		ic.add(JOB_SEQ, new LongNumeralCheck());
		ic.populate();

		try (Jdbc conn = JdbcSource.getConnection()) {
			JobStatus js = Factory.create(JobStatus.class);
			Job job = js.getJob(conn, model.getNumber(JOB_SEQ).longValue());

			SessionUser su = model.getSessionUser();
			if (Objects.equals(job.getUid(), su.getUid())) {
				// 中止処理呼出
				JobRequestor.cancelJob(model.getNumber(JOB_SEQ).longValue());
			}
		}
		return ID_OK;
	}
}
